﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using log4net;
using UtilLib;
using System;

namespace UtilPublic.ServerLoger
{
    /// <summary>
    /// 日志等级
    /// </summary>
    public enum ELogLevel
    {
        /// <summary>
        /// 调试
        /// </summary>
        Debug = 1,
        /// <summary>
        /// 常规信息
        /// </summary>
        Info = 2,
        /// <summary>
        /// 警告
        /// </summary>
        Warn = 3,
        /// <summary>
        /// 错误
        /// </summary>
        Error = 4,
        /// <summary>
        /// 严重错误
        /// </summary>
        Fatal = 5,
        /// <summary>
        /// 仅显示调试
        /// </summary>
        OnlyDebug = 6,
        /// <summary>
        /// 不显示
        /// </summary>
        None = 7,
    }

    /// <summary>
    /// 日志系统
    /// </summary>
    public static partial class loger
    {
        /// <summary>
        /// 文件日志
        /// </summary>
        private static ILog m_logFile = null;

        /// <summary>
        /// 命令行日志
        /// </summary>
        private static ILog m_logConsole = null;

        /// <summary>
        /// 日志等级
        /// </summary>
        private static ELogLevel m_logLevel = ELogLevel.Debug;

        /// <summary>
        /// 初始化日志文件
        /// </summary>
        public static void CreateLoger()
        {
            if (m_logFile == null)
            {
                m_logFile = log4net.LogManager.Exists("log_file");
            }
            if (m_logConsole == null)
            {
                m_logConsole = log4net.LogManager.Exists("log_console");
            }
        }

        /// <summary>
        /// 获取日志等级
        /// </summary>
        /// <returns></returns>
        public static ELogLevel GetLevel()
        {
            return m_logLevel;
        }

        /// <summary>
        /// 修改日志等级
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static ELogLevel ChangeLevel(int level)
        {
            return ChangeLevel((ELogLevel)level);
        }

        /// <summary>
        /// 修改日志等级
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static ELogLevel ChangeLevel(ELogLevel level)
        {
            if (level >= ELogLevel.Debug && level <= ELogLevel.None)
            {
                m_logLevel = level;
            }
            else
            {
                m_logLevel = ELogLevel.None;
            }
            return m_logLevel;
        }

        /// <summary>
        /// 判断是否debug
        /// </summary>
        /// <returns></returns>
        public static bool IsDebug()
        {
            return (m_logLevel >= ELogLevel.Debug);
        }

        /// <summary>
        /// 修改日志等级 外网发布版本
        /// </summary>
        /// <returns></returns>
        public static ELogLevel ChangeLevelRelease()
        {
            return ChangeLevel(ELogLevel.Warn);
        }


        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">标题</param>
        /// <param name="ex">异常</param>
        public static void Debug(object titlname, Exception ex)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.Green;
            if (m_logFile == null) return;
            if (m_logFile.IsDebugEnabled)
            {
                m_logFile.Debug(titlname, ex);
            }
            if ((m_logConsole.IsDebugEnabled && m_logLevel <= ELogLevel.Debug) || m_logLevel == ELogLevel.OnlyDebug)
            {
                m_logConsole.Debug(titlname, ex);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">信息</param>
        public static void Debug(object titlname)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.Green;
            if (m_logFile == null) return;
            if (m_logFile.IsDebugEnabled)
            {
                m_logFile.Debug(titlname);
            }
            if ((m_logConsole.IsDebugEnabled && m_logLevel <= ELogLevel.Debug) || m_logLevel == ELogLevel.OnlyDebug)
            {
                m_logConsole.Debug(titlname);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">标题</param>
        /// <param name="ex">异常</param>
        public static void Info(object titlname, Exception ex)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            if (m_logFile == null) return;
            if (m_logFile.IsInfoEnabled)
            {
                m_logFile.Info(titlname, ex);
            }
            if (m_logConsole.IsInfoEnabled && m_logLevel <= ELogLevel.Info)
            {
                m_logConsole.Info(titlname, ex);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">信息</param>
        public static void Info(object titlname)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            if (m_logFile == null) return;
            if (m_logFile.IsInfoEnabled)
            {
                m_logFile.Info(titlname);
            }
            if (m_logConsole.IsInfoEnabled && m_logLevel <= ELogLevel.Info)
            {
                m_logConsole.Info(titlname);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">标题</param>
        /// <param name="ex">异常</param>
        public static void Warn(object titlname, Exception ex)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.Magenta;
            if (m_logFile == null) return;
            if (m_logFile.IsWarnEnabled)
            {
                m_logFile.Warn(titlname, ex);
            }
            if (m_logConsole.IsWarnEnabled && m_logLevel <= ELogLevel.Warn)
            {
                m_logConsole.Warn(titlname, ex);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">信息</param>
        public static void Warn(object titlname)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.Magenta;
            if (m_logFile == null) return;
            if (m_logFile.IsWarnEnabled)
            {
                m_logFile.Warn(titlname);
            }
            if (m_logConsole.IsWarnEnabled && m_logLevel <= ELogLevel.Warn)
            {
                m_logConsole.Warn(titlname);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">标题</param>
        /// <param name="ex">异常</param>
        public static void Error(object titlname, Exception ex)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.Yellow;
            if (m_logFile == null)
            {
                Util.ShowError(titlname);
                return;
            }
            if (m_logFile.IsErrorEnabled)
            {
                m_logFile.Error(titlname, ex);
            }
            if (m_logConsole.IsErrorEnabled && m_logLevel <= ELogLevel.Error)
            {
                m_logConsole.Error(titlname, ex);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">信息</param>
        public static void Error(object titlname)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.Yellow;
            if (m_logFile == null)
            {
                Util.ShowError(titlname);
                return;
            }
            if (m_logFile.IsErrorEnabled)
            {
                m_logFile.Error(titlname);
            }
            if (m_logConsole.IsErrorEnabled && m_logLevel <= ELogLevel.Error)
            {
                m_logConsole.Error(titlname);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">标题</param>
        /// <param name="ex">异常</param>
        public static void Fatal(object titlname, Exception ex)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.White;
            if (m_logFile == null)
            {
                Util.ShowError(titlname);
                return;
            }
            if (m_logFile.IsFatalEnabled)
            {
                m_logFile.Fatal(titlname, ex);
            }
            if (m_logConsole.IsFatalEnabled && m_logLevel <= ELogLevel.Fatal)
            {
                m_logConsole.Fatal(titlname, ex);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        /// <summary>
        /// BUG日志输出
        /// </summary>
        /// <param name="titlname">信息</param>
        public static void Fatal(object titlname)
        {
            CreateLoger();
            Console.ForegroundColor = ConsoleColor.White;
            if (m_logFile == null)
            {
                Util.ShowError(titlname);
                return;
            }
            if (m_logFile.IsFatalEnabled)
            {
                m_logFile.Fatal(titlname);
            }
            if (m_logConsole.IsFatalEnabled && m_logLevel <= ELogLevel.Fatal)
            {
                m_logConsole.Fatal(titlname);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        #region 临时调试开关
        /// <summary>
        /// 
        /// </summary>
        public static bool 临时调试开关 = true;
        /// <summary>
        /// 临时调试打印
        /// </summary>
        /// <param name="obj"></param>
        public static void DDD(object obj)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            if (临时调试开关)
            {
                Console.Write($"{obj}\n");
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }
        /// <summary>
        /// 临时调试打印
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ex"></param>
        public static void DDD(object obj, Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            if (临时调试开关)
            {
                Console.Write($"{obj}\n");
                Console.Write($"{ex}\n");
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        } 
        #endregion

    }
}